package com.gitee.kenewstar.migration.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.kenewstar.migration.constants.Const;
import com.gitee.kenewstar.migration.entity.Message;
import com.gitee.kenewstar.migration.entity.Prompt;
import com.gitee.kenewstar.migration.mapper.MessageMapper;
import com.gitee.kenewstar.migration.mapper.PromptMapper;
import com.gitee.kenewstar.migration.service.MulLanguageService;
import com.gitee.kenewstar.migration.util.Util;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author kenewstar
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MulLanguageServiceImpl implements MulLanguageService {

    private final PromptMapper promptMapper;
    private final MessageMapper messageMapper;

    @Override
    @DS(Const.DEV)
    public List<Prompt> devSelectPromptByLike(List<String> promptKey) {
        if (CollUtil.isEmpty(promptKey)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<Prompt> queryWrapper = new QueryWrapper<>();
        promptKey.forEach(key -> queryWrapper.or().likeRight("prompt_key", key));
        return this.promptMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<Message> devSelectMessageByLike(List<String> prefixCode) {
        if (CollUtil.isEmpty(prefixCode)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        prefixCode.forEach(code -> queryWrapper.or().likeRight("code", code));
        return this.messageMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<Prompt> devSelectPromptList(List<Prompt> list) {
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<Prompt> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("prompt_key", list.stream().map(Prompt::getPromptKey).collect(Collectors.toSet()));
        queryWrapper.in(Const.TENANT_ID, list.stream().map(Prompt::getTenantId).collect(Collectors.toSet()));
        List<Prompt> promptList = this.promptMapper.selectList(queryWrapper);
        Map<String, String> map = list.stream().collect(Collectors.toMap(x -> x.getPromptKey() + "-" + x.getTenantId(), Prompt::getPromptKey));
        return promptList.stream().filter(x -> Objects.nonNull(map.get(x.getPromptKey() + "-" + x.getTenantId()))).collect(Collectors.toList());
    }

    @Override
    @DS(Const.DEV)
    public List<Message> devSelectMessageList(List<Message> list) {
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code", list.stream().map(Message::getCode).collect(Collectors.toSet()));
        return this.messageMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertPrompt(Prompt prompt) {
        prompt.setPromptId(null);
        prompt.setTenantId(Util.getTenant(prompt.getTenantId()));
        this.promptMapper.insert(prompt);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertMessage(Message message) {
        message.setMessageId(null);
        this.messageMapper.insert(message);
    }
}
